/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kom;

import java.util.ArrayList;
import java.util.List;
import kom.item.Item;
import kom.item.resource.Resource;
import kom.item.resource.ResourceItem;

/**
 *
 * @author Nathan
 */
public class Inventory {

    public List<Item> items = new ArrayList<Item>();

    public void add(Item item) {
        add(items.size(), item);
    }

    public void add(int slot, Item item) {
        if (item instanceof ResourceItem) {
            ResourceItem toTake = (ResourceItem) item;
            ResourceItem has = findResource(toTake.resource);
            if (has == null) {
                items.add(slot, toTake);
            } else {
                has.count += toTake.count;
            }
        } else {
            items.add(slot, item);
        }
    }

    private ResourceItem findResource(Resource resource) {
        for (int i = 0; i < items.size(); i++) {
            if (items.get(i) instanceof ResourceItem) {
                ResourceItem has = (ResourceItem) items.get(i);
                if (has.resource == resource) {
                    return has;
                }
            }
        }
        return null;
    }
}
